package com.lmy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmy.common.utils.PageUtils;
import com.lmy.common.utils.Query;
import com.lmy.product.entity.BrandEntity;
import com.lmy.product.entity.CategoryBrandRelationEntity;
import com.lmy.product.entity.CategoryEntity;
import com.lmy.product.mapper.BrandDao;
import com.lmy.product.mapper.CategoryBrandRelationDao;
import com.lmy.product.mapper.CategoryDao;
import com.lmy.product.service.BrandService;
import com.lmy.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils catelogList(Map<String, Object> params, Long brandId) {
        return null;
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //获取品牌id
        Long brandId = categoryBrandRelation.getBrandId();
        //获取分类id
        Long catelogId = categoryBrandRelation.getCatelogId();
        //根据品牌id查询品牌名称
        BrandEntity brandEntity = brandDao.selectById(brandId);
        //根据分类id查询分类名称
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    @Transactional
    public void udpateBrand(BrandEntity brand) {
        Long brandId = brand.getBrandId();
        String name = brand.getName();
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        categoryBrandRelationEntity.setBrandId(brandId);
        this.update(categoryBrandRelationEntity, new QueryWrapper<CategoryBrandRelationEntity>().
                eq("brand_id", brandId));

    }

    @Override
    @Transactional
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandRelation(Long catId) {

        List<CategoryBrandRelationEntity> brandRelationEntities = categoryBrandRelationDao.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId));
        List<BrandEntity> collect = brandRelationEntities.stream().map(item -> {
            return brandService.getById(item.getBrandId());
        }).collect(Collectors.toList());
        return collect;
    }

}